package com.lktx.bbfactory.common.hander;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;

public class ProductTemplateDropdownWriteHandler implements SheetWriteHandler {

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {}

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 第3列（从0开始计数，即索引为2）设置下拉内容
        String[] dropdownOptions = {"HIC", "高硅氧", "涂层", "结构复材","电气","力热"};
        DataValidationConstraint constraint = helper.createExplicitListConstraint(dropdownOptions);
        // 设置适用的单元格范围，这里设置为第2列（第三列），从第2行到第100行（排除表头）
        CellRangeAddressList addressList = new CellRangeAddressList(1, 100, 2, 2);
        DataValidation validation = helper.createValidation(constraint, addressList);
        // 处理 Excel 不兼容性
        if (validation instanceof XSSFDataValidation) {
            validation.setSuppressDropDownArrow(true);
            validation.setShowErrorBox(true);
        }
        sheet.addValidationData(validation);
    }
}
